
class Player {

    mainRole: Role; //角色

    physical: PhysicalEngine;//物理引擎

    box: Box;//脚下的跳台

    isdead: boolean = false;//是否死亡

    is_move:boolean =true;

    isjumping: boolean = false;//是不是在跳跃过程中

    startjump: boolean = false;//控制起跳

    isdropping: boolean = false;//正在下落

    lastposition_X: number;

    start_y: number = 0;

    end_y: number = 0;

    box_speed_intex:number=1;

    


    dialog: dilog_and_button; //到达故事点后呈现得对话框和选项


    init_player(){

        this.isdead=false;
        this.isjumping=false;
        this.startjump=false;
        this.isdropping=false;
        this.start_y=0;
        this.end_y=0;
        this.box_speed_intex=1;
        this.mainRole.move_direction=true;
        this.mainRole.state=0;
        this.is_move=true;

    }
    get movespeed() {//获取移动速度
        return this.mainRole.speedintex_role * this.physical.standard_movespeed*this.box_speed_intex;
    }

    walk(transform: Transform, duringTime, direction: boolean, iscollider: boolean) {//true:right,false:left

        this.lastposition_X = transform.x;
        if (direction) {
            transform.x += player.movespeed * duringTime / 500;
        }
        else {
            transform.x -= player.movespeed * duringTime / 500;
        }
        if (iscollider) {
            transform.x = this.lastposition_X;
        }

    }
    jump(transform: Transform, duringtime, iscollider) {


        transform.y -= this.physical.Jumpspeed *this.box.jumpintex_box *this.mainRole.jumpspeedindex_role*duringtime / 500;

        this.physical.Jumpspeed = this.physical.Jumpspeed + this.physical.GravityAcceler * duringtime / 500;
        if (iscollider) {
            this.physical.Jumpspeed = 0;
        }
        if (this.physical.Jumpspeed <= 0) {
            this.physical.Jumpspeed = this.physical.JumpInitialSpeed;
            this.isjumping = false;
        }
    }

    drop(transform: Transform, duringTime, iscollider) {

        if (!iscollider) {
            transform.y += 2000 * duringTime / 600 * duringTime / 600;
            this.isdropping = true;
        } else {

            this.isdropping = false;

        }
    }

}

class Role {

    move_direction: boolean = true;//true为左边，false为右边。
    state: number = 0;

    get speedintex_role() {//角色速度系数（角色）

        return 1;
    }

    get jumpspeedindex_role() {//起跳跳跃速度系数（角色）
        if (this.state == 0) {
            return 0;
        }
        if (this.state == 1) {
            return 1;
        }
        if (this.state == 2) {
            return 1.25;
        }
        if (this.state == 3) {
            return 0;
        }
        if (this.state == 4) {
            return 0;
        }
    }

    get image() {
        if (this.state == 0) {
            if (this.move_direction) {
                return 0;
            } else {
                return 1;
            }
        }
        if (this.state == 1) {
            if (this.move_direction) {
                return 2;
            } else {
                return 3;
            }

        }
        if (this.state == 2) {
            if (this.move_direction) {
                return 4;
            } else {
                return 5;
            }
        }
        if (this.state == 3) {
            if (this.move_direction) {
                return 6;
            } else {
                return 7;
            }
        }
        if (this.state == 4) {
            return 8;
        }

    }


}




class PhysicalEngine {

    standard_movespeed = 75;//基础移动速度

    GravityAcceler = -700; //重力加速度

    Jumpspeed = 500;//跳跃速度

    JumpInitialSpeed = 500//起跳初速度

    GravityFactor = 0.2;//下落速度系数




}

class dilog_and_button { //控制画面选项和对话框，处理人生不同阶段的选择。


    story: GameObject[] = [];//关卡，按序列加入结点及后续perfab。
    constructor(private background: GameObject, private leftbutton_image: GameObject, private rightbutton_image: GameObject, 
        private leftbutton_text: GameObject,private rightbutton_text: GameObject,private num: number) { }

    get stage() {
        if (player.mainRole.state == 1) {
            return "Young";
        }
        if (player.mainRole.state == 2) {
            return "Midage";
        }
        if (player.mainRole.state == 3) {
            return "Old";
        }
    }
    async set_img_image_text() {
        this.leftbutton_image.getBehaviour(ImageRenderer).image = "assets/uibutton_" + this.stage + ".png";
        this.rightbutton_image.getBehaviour(ImageRenderer).image = "assets/uibutton_" + this.stage + ".png";
        this.background.getBehaviour(ImageRenderer).image = trigger[this.num][0];
        this.leftbutton_text.getBehaviour(TextRenderer).text=trigger[this.num][1];
        this.rightbutton_text.getBehaviour(TextRenderer).text=trigger[this.num][2];

    }



    async execute() {

        await this.set_img_image_text();
        const result = await this.untilClick();
        if (result == 0) {
            for(let gameobject of changeGameobjet[trigger[this.num][3]]){
                gameobject.setActive(true);
            }
            player.is_move=true;
            console.log("test for dialog and button");

        }
        else {
            for(let gameobject of changeGameobjet[trigger[this.num][4]]){
                gameobject.setActive(true);
           
            }
            player.is_move=true;
        }



    }

    private untilClick() {
        return new Promise<number>((resolve, reject) => {
            this.leftbutton_image.onClick = () => {
                resolve(0);
            };
            this.rightbutton_image.onClick = () => {
                resolve(1);
            };
        });
    }

}

const player = new Player();//初始化人物类
player.physical = new PhysicalEngine();
player.mainRole = new Role();
player.mainRole.state = 0;



